﻿using Autofac;
using LS.Entitys;
using LS.Entitys.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LS.Api.Helper
{
    /// <summary>
    /// ContainerBuilder 帮助类
    /// </summary>
    public class ContainerBuilderHelper
    {
        private static ContainerBuilderHelper _instance;
        /// <summary>
        /// 组建注册容器
        /// </summary>
        IContainer container;
        public static ContainerBuilderHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ContainerBuilderHelper();
                }
                return _instance;
            }
        }

        public ContainerBuilderHelper()
        {
            Autofac.ContainerBuilder builder = new Autofac.ContainerBuilder();
            builder.RegisterType<LS.Cores.SysLoginLogService>().As<LS.Cores.ISysLoginLog>();
            builder.RegisterType<LS.Cores.SysOperateLogService>().As<LS.Cores.ISysOperateLog>();
            builder.RegisterType<LS.Cores.SysMenuService>().As<LS.Cores.ISysMenu>();
            builder.RegisterType<Cores.SysDictionaryService>().As<Cores.ISysDictionary>();
            builder.RegisterType<LS.Cores.SysFunctionService>().As<LS.Cores.ISysFunction>();
            builder.RegisterType<LS.Cores.SysConfigurationService>().As<Cores.ISysConfiguration>();
            builder.RegisterType<LS.Cores.SysFileService>().As<LS.Cores.ISysFile>();
            builder.RegisterType<LS.Cores.SysUserService>().As<LS.Cores.ISysUser>();
            builder.RegisterType<LS.Cores.CmsNewsCategoryService>().As<LS.Cores.ICmsNewsCategory>();

            container = builder.Build();
        }
        /// <summary>
        /// 添加登录日志信息
        /// </summary>
        /// <param name="apiLogdata"></param>
        /// <returns></returns>
        public long AddLoginLog(SysLoginLogEntity apiLogdata)
        {
            return container.Resolve<LS.Cores.ISysLoginLog>().Insert(apiLogdata);
        }
        /// <summary>
        /// 添加操作日志信息
        /// </summary>
        /// <param name="apiLogdata"></param>
        /// <returns></returns>
        public long AddOperateLoginLog(SysOperateLogEntity apiLogdata)
        {
            return container.Resolve<LS.Cores.ISysOperateLog>().Insert(apiLogdata);
        }
        /// <summary>
        /// 根据用户名Id获取菜单
        /// </summary>
        /// <param name="userCode">用户名称</param>
        /// <returns></returns>
        public IList<SysMenuEntity> GetUserPartMenu(string userId)
        {
            IList<SysMenuEntity> menulist = null;
            var iSysMenu = container.Resolve<LS.Cores.ISysMenu>();
            if ("999999" == userId)//管理员
            {
                menulist = iSysMenu.Query(" and isvisible=1 and isenable=1 and Isdel=0 ");
            }
            else
            {
                menulist = iSysMenu.GetUserPartMenu(userId, 0);
            }
            return menulist;
        }
        /// <summary>
        /// 根据类型获取角色的授权用户
        /// </summary>
        /// <param name="roletype">1=SXSPYJ 2 =SXSPEJ 3=SXSPSJ 4=SXSPSSJ </param>
        /// <returns></returns>
        public List<SysUserRoleEntity> GetUserByRole(int roletype)
        {
            List<SysUserRoleEntity> list = new List<SysUserRoleEntity>();
            string rolecode = string.Empty;
            switch (roletype)
            {
                case 1:
                    rolecode = "SXSPYJ";
                    break;
                case 2:
                    rolecode = "SXSPEJ";
                    break;
                case 3:
                    rolecode = "SXSPSJ";
                    break;
                case 4:
                    rolecode = "SXSPSSJ";
                    break;
                default:
                    break;
            }
            var userservice = container.Resolve<LS.Cores.ISysUser>();
            if (!string.IsNullOrEmpty(rolecode))
            {
                list = userservice.GetRoleUser(" and rolecode= '" + rolecode + "'");
            }
            return list;
        }

        public SysFunctionEntity GetFunction(string funKey)
        {
            var funservice = container.Resolve<LS.Cores.ISysFunction>();
            return funservice.Query("   and funcode='" + funKey + "'").FirstOrDefault();
        }

        public string GetConfigValueByCode(string code)
        {
            string config = string.Empty;
            var configService = container.Resolve<LS.Cores.ISysConfiguration>();
            var configValue = configService.GetByCode(code);

            if (configValue != null)
            {
                config = configValue.Args;
            }
            return config;
        }

        /// <summary>
        /// 根据字典值获取名称
        /// </summary>
        /// <param name="dval"></param>
        /// <param name="dicType"></param>
        /// <returns></returns>
        public string GetDictionaryName(string dval, SysDictionaryType dicType)
        {
            var dicservice = container.Resolve<LS.Cores.ISysDictionary>();

            var diclist = dicservice.Query(" and isdel=0 and  isenable=1 and Dvalue='" + dval + "' and  ParentId =( select top 1 id From sys_dictionary where  ParentId is null and dictype='" + dicType.ToString() + "')  ");
            if (diclist != null && diclist.Count > 0)
            {
                return diclist.FirstOrDefault().Dname;
            }
            return string.Empty;
        }
        /// <summary>
        /// 根据字典值获取名称(多个)
        /// </summary>
        /// <param name="dvals"></param>
        /// <param name="dicType"></param>
        /// <returns></returns>
        public List<string> GetDictionaryNameList(string dvals, SysDictionaryType dicType)
        {
            var dicservice = container.Resolve<LS.Cores.ISysDictionary>();
            var list = dvals.Split(',');
            List<string> ret = new List<string>();
            foreach (var dval in list)
            {
                var diclist = dicservice.Query(" and isdel=0 and  isenable=1 and Dvalue='" + dval + "' and  ParentId =( select top 1 id From sys_dictionary where  ParentId is null and dictype='" + dicType.ToString() + "')  ");
                if (diclist != null && diclist.Count > 0)
                {
                    ret.Add(diclist.FirstOrDefault().Dname);
                }
            }
            return ret;
        }
        /// <summary>
        /// 根据字典类型下所有标签
        /// </summary>
        /// <param name="dicType"></param>
        /// <returns></returns>
        public List<SysDictionaryEntity> GetDictionaryTagByDicType(SysDictionaryType dicType)
        {
            var dicservice = container.Resolve<LS.Cores.ISysDictionary>();
            return dicservice.Query(" and isdel=0 and  isenable=1  and  ParentId =( select top 1 id From sys_dictionary where  ParentId is null and dictype='" + dicType.ToString() + "')  ").ToList();
        }

        /// <summary>
        /// 根据字典类型下所有标签
        /// </summary>
        /// <param name="dicType"></param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public List<SysDictionaryEntity> GetDictionaryTagByDicType(SysDictionaryType dicType, string strWhere)
        {
            var dicservice = container.Resolve<LS.Cores.ISysDictionary>();
            return dicservice.Query(strWhere + " and isdel=0 and  isenable=1  and  ParentId =( select top 1 id From sys_dictionary where  ParentId is null and dictype='" + dicType.ToString() + "')  ").ToList();
        }

        /// <summary>
        /// 根据id 获取文件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<SysFileEntity> GetFileList(string ids)
        {
            ids = (ids ?? "").Trim(',');
            if (string.IsNullOrEmpty(ids))
            {
                return new List<SysFileEntity>();
            }
            var fileservice = container.Resolve<LS.Cores.ISysFile>();
            return fileservice.Query(" and id in( " + ids + ")").ToList();
        }
        public SysFileEntity GetFile(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return null;
            }
            var fileservice = container.Resolve<LS.Cores.ISysFile>();
            return fileservice.Query(" and id in( " + id + ")").FirstOrDefault();
        }
        /// <summary>
        /// 获取id及其子类id字符串,最多查4层
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public string GetNewsCategoryIds(int categoryId)
        {
            string ids = $"{categoryId}";
            var service = container.Resolve<LS.Cores.ICmsNewsCategory>();
            var list = service.Query($"and isDel=0 and CategoryStatus=1");
            foreach (var item1 in list.Where(a => a.ParentId == categoryId))
            {
                ids += "," + item1.Id;
                foreach (var item2 in list.Where(a => a.ParentId == item1.Id))
                {
                    ids += "," + item2.Id;
                    foreach (var item3 in list.Where(a => a.ParentId == item2.Id))
                    {
                        ids += "," + item3.Id;
                    }
                }
            }
            return ids;
        }
        /// <summary>
        /// 获取订单编号
        /// </summary>
        /// <param name="type">0=工单 1=客户</param>
        /// <returns></returns>
        public string GetWorkOrder(int type)
        {
            var configurationservice = container.Resolve<LS.Cores.ISysConfiguration>();
            string billname = string.Empty;
            switch (type)
            {
                case 0:
                    billname = "WO";
                    break;
                case 1:
                    billname = "CUT";
                    break;
                default:
                    break;
            }
            return configurationservice.GetBillNumber(billname, billname);
        }
    }
}